open class NetworkTask : NSObject {
  
  let httpUrl: String
  
  let tag: String
  
  var method: HttpMethod
  
  var shouldCached: Bool
  
  var debugTag: String?
  
  var networkParameter: Parameter?
  
  var callTime:TimeInterval = 0
  
  var needDelayFlag = false
  
  var interf:TimeInterval = 0.8

  var targetTask:AnyObject? = nil
  
  var responceDelegate:TXNetworkResponce? = nil
  
  
  override open var description: String {
    return "NetworkTask:[\(httpUrl),\(method),\(networkParameter?.description)]"
  }
  
  init(httpUrl: String) {
    self.httpUrl = httpUrl
    self.tag = httpUrl
    self.method = HttpMethod.GET
    self.shouldCached = false
    if NetworkCoreDebug {
      self.debugTag = httpUrl
    }
    
  }
  
  convenience init(fullURL: String, method: HttpMethod) {
    self.init(httpUrl: fullURL)
    self.method = method
  }
  
  convenience init(url: String, method: HttpMethod) {
    self.init(httpUrl: NetworkConfig.baseURL + url)
    self.method = method
  }
  
  // 拼接参数
  func addParameter(key k: String, value: String?) {
    
    guard let inputValue = value else {
      return
    }
    if networkParameter == nil {
      networkParameter = Parameter()
    }
    networkParameter!.params[k] = inputValue
  }
  
  func addHeader(key k: String, value: String?) {
    
    guard let inputValue = value else {
      return
    }
    if networkParameter == nil {
      networkParameter = Parameter()
    }
    networkParameter!.headers[k] = inputValue
  }
  
  fileprivate func configSign() {
  }
  
  deinit {
    TXLogDebug("Task将要释放：\(self.httpUrl)")
  }
  
  func generatorHttpGetURL()->String {
    var paramsStr = "?"
    if let params = networkParameter?.params {
      
      for (key,value) in params {
        paramsStr  = paramsStr + "\(key)=\(value)&"
      }
      if paramsStr.contains("&") {
        paramsStr.remove(at: paramsStr.endIndex)
      }
      
    }
    return httpUrl + paramsStr
  }
  
  // 取消网络请求 , 需要复写此方法
  func cancle(){
    NetworkTaskManager.sharedInstance.removeTask(self)
  }
}



class Parameter {
  
  var params: Dictionary<String, String>
  
  var headers: Dictionary<String, String>
  
  init() {
    
    self.params = [String: String]()
    self.headers = [String: String]()
  }
  
  var description: String {
    return "参数:[\(params)]"
  }
}
